/**
 *
 */
package com.nest.WorkNest.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nest.WorkNest.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

import java.util.ArrayList;
import java.util.HashMap;

public class RecommendAdapter extends BaseAdapter {

    private ArrayList<HashMap<String, String>> dataList;
    private Context context;
    private ImageLoader imageLoader;
    private DisplayImageOptions options;

    public RecommendAdapter(Context context, ArrayList<HashMap<String, String>> dataList) {
        this.dataList = dataList;
        this.context = context;

        imageLoader = ImageLoader.getInstance();
        imageLoader.init(ImageLoaderConfiguration.createDefault(context));
        options = new DisplayImageOptions.Builder().showStubImage(R.drawable.empty_photo) // 设置图片下载期间显示的图片
                .showImageForEmptyUri(R.drawable.empty_photo) // 设置图片Uri为空或是错误的时候显示的图片
                .showImageOnFail(R.drawable.empty_photo) // 设置图片加载或解码过程中发生错误显示的图片
                .cacheInMemory(true) // 设置下载的图片是否缓存在内存中
                .cacheOnDisc(true) // 设置下载的图片是否缓存在SD卡中
                .displayer(new RoundedBitmapDisplayer(20)) // 设置成圆角图片
                .build(); // 创建配置过得DisplayImageOption对象
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public Object getItem(int position) {
        return dataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.items_listview_recommend,
                    null);
            holder.proName = (TextView) convertView.findViewById(R.id.recommend_proname);
            holder.orderCount = (TextView) convertView.findViewById(R.id.recommend_order_count);
            holder.finishCount = (TextView) convertView
                    .findViewById(R.id.recommend_order_finish_count);
            holder.price = (TextView) convertView.findViewById(R.id.recommend_price);
//            holder.push = (TextView) convertView.findViewById(R.id.recommend_push);
            holder.recommendImg = (ImageView) convertView.findViewById(R.id.recommend_img);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        HashMap<String, String> map = dataList.get(position);
        holder.proName.setText(map.get("productName"));
        holder.orderCount.setText(map.get("bookedQuantity"));
//        holder.finishCount.setText(map.get("finishCount"));
        holder.price.setText(map.get("quotation"));
//        holder.push.setText(map.get("push"));
        imageLoader.displayImage(map.get("mainImange"), holder.recommendImg, options);
        return convertView;
    }

    private class ViewHolder {
        TextView proName, orderCount, finishCount, price, push;
        ImageView recommendImg;
    }

    public void update(ArrayList<HashMap<String, String>> dataList) {
        this.dataList = dataList;
        notifyDataSetChanged();
    }
}
